package com.nlscan.qdkit.ui.publico.art.mvvm

import android.app.ProgressDialog
import android.content.Context
import android.os.Bundle
import android.widget.ScrollView
import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.DialogFragment
import androidx.viewbinding.ViewBinding
import com.nlscan.qdkit.common.core.app.App
import com.quvii.qvmvvm.core.base.KtxBaseView
import com.quvii.qvmvvm.core.base.KtxBaseViewModel

/**
 * <pre>
 *  author: lin.qing
 *  time  : 2023/3/30
 *  desc  :
 * </pre>
 */
abstract class BaseMvvmDialogFragment<T : ViewBinding> : DialogFragment(), KtxBaseView {

    var binding: T? = null

    protected var mDialog: AlertDialog? = null

    //    var mContext : Context? = null
    private var progressDialog: ProgressDialog? = null

    /**
     * 返回根布局
     * -- 当前测试没有效果
     */
    protected fun getRootScrollView(): ScrollView {
        val scrollView = ScrollView(requireActivity().applicationContext)
//        scrollView.isScrollbarFadingEnabled = false
        return scrollView
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
//        val inflater = LayoutInflater.from(requireContext())
//        getViewBinding(inflater, container).also { binding = it }.root
//        initData()
        initViewModel(startObserve(), this)
    }

//    abstract fun initData()

    abstract fun startObserve(): KtxBaseViewModel


    override fun showOrHideLoading(isShow: Boolean) {
        if (isShow) {
            if (progressDialog == null) {
                progressDialog = ProgressDialog(context).apply {
                    setCanceledOnTouchOutside(false)
                    setProgressStyle(ProgressDialog.STYLE_SPINNER)
                }
            }
            progressDialog?.show()
        } else {
            progressDialog?.apply {
                if (isShowing) {
                    hide()
                }
            }
        }
    }

    override fun showResult(code: Int) {
    }

    override fun onShowRefresh(isShow: Boolean) {

    }


    // 使用 onCreateDialog，不使用 onCreateView
    /*
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return getViewBinding(inflater, container).also { binding = it }.root
    }
     */

//    abstract fun getViewBinding(
//        inflater: LayoutInflater,
//        container: ViewGroup?
//    ): T

//    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
//        initView(savedInstanceState)
//        initData()
//        super.onViewCreated(view, savedInstanceState)
//    }

//    abstract fun initView(savedInstanceState: Bundle?)
//    abstract fun initData()

    override fun onDestroyView() {
        super.onDestroyView()
        binding = null
    }

    protected fun getAppContext(): Context = App.getInstance()


}